package com.team.arms.service_management_before.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.arms.common.PageVO;
import com.team.arms.common.annotation.Log;
import com.team.arms.common.ry.core.controller.BaseController;
import com.team.arms.common.ry.core.domain.AjaxResult;
import com.team.arms.common.ry.core.page.TableDataInfo;
import com.team.arms.common.ry.enums.BusinessType;
import com.team.arms.service_management_before.domain.SpecificationGuidelines;
import com.team.arms.service_management_before.service.SpecificationGuidelinesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author yjh
 * @create 2022-06-24-15:03
 * @description:
 */
@Api( tags = "规范指引")
@RestController
@RequestMapping("/serviceManagementBefore/SpecificationGuidelines")
public class SpecificationGuidelinesController extends BaseController {

    @Autowired
    private SpecificationGuidelinesService specificationGuidelinesService;
    /**
     * 查询规范指引
     * @return
     */
    @ApiOperation("查询规范指引")
    @GetMapping("/select")
    public AjaxResult selectPage(@ApiParam("选中的页数") @RequestParam("pageNum") Integer pageNum ,
                                 @ApiParam("页面显示的数据量") @RequestParam("pageSize") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<SpecificationGuidelines> list = specificationGuidelinesService.list();
        PageInfo<SpecificationGuidelines> pageInfo = new PageInfo<SpecificationGuidelines>(list);
        PageVO<SpecificationGuidelines> specificationGuidelinesPageVO = new PageVO<>();
        BeanUtils.copyProperties(pageInfo, specificationGuidelinesPageVO);
        specificationGuidelinesPageVO.setResult(pageInfo.getList());
        specificationGuidelinesPageVO.setPage(pageNum);
        return AjaxResult.success(specificationGuidelinesPageVO);
    }

    /**
     * 根据id查询规范指引
     * @param id
     * @return
     */
    @ApiOperation("根据id查询规范指引")
    @GetMapping("/selectById")
    public AjaxResult selectById( @RequestParam(value = "specification_guidelines_id")String id){
        return AjaxResult.success(specificationGuidelinesService.selectById(id));
    }

    /**
     *
     * @param field
     * @param content
     * @return
     */
    @ApiOperation("根据对应字段查询规范指引")
    @GetMapping("/selectByFile")
    public AjaxResult selectByField(@ApiParam("查询的字段")@RequestParam(value = "field") String field ,
                                    @ApiParam("查询的内容")@RequestParam(value = "content")String content){
        field = humpToUnderline(field);
        return AjaxResult.success(specificationGuidelinesService.selectByFiled(field,content));
    }
    public static String humpToUnderline(String str) {
        String regex = "([A-Z])";
        Matcher matcher = Pattern.compile(regex).matcher(str);
        while (matcher.find()) {
            String target = matcher.group();
            str = str.replaceAll(target, "_"+target.toLowerCase());
        }
        return str;
    }
    /**
     * 新增规范指引
     * @param specificationGuidelines
     */
    @ApiOperation("新增规范指引")
    @PostMapping("/install")
    public AjaxResult installSpecificationGuidelines(@RequestBody SpecificationGuidelines specificationGuidelines){
        return toAjax(specificationGuidelinesService.insertAll(specificationGuidelines));
    }

    /**
     * 修改规范指引
     * @param id
     * @param specificationGuidelines
     * @return
     */
    @ApiOperation("修改规范指引")
    @Log(title = "修改规范指引", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult updateSpecificationGuidelines(
            @RequestParam(value = "specification_guidelines_id")String id,
            @RequestBody SpecificationGuidelines specificationGuidelines){
        QueryWrapper<SpecificationGuidelines> wrapper = new QueryWrapper();
        wrapper.eq("specification_guidelines_id",id);
        SpecificationGuidelines byId = specificationGuidelinesService.getById(id);
        return toAjax(specificationGuidelinesService.update(specificationGuidelines,wrapper));
    }

    /**
     * 删除规范指引
     * @param id
     * @return
     */
    @ApiOperation("批量删除规范指引")
    @DeleteMapping("/delete")
    public AjaxResult deleteSpecificationGuidelines(@RequestParam("specification_guidelines_id") List<String> id){
        return toAjax(specificationGuidelinesService.deleteByClothingIds(id));
    }

    @ApiOperation("删除规范指引")
    @DeleteMapping("/deleteById")
    public AjaxResult deleteById(@RequestParam("specification_guidelines_id") String id){
        return toAjax(specificationGuidelinesService.deleteByClothingId(id));
    }

    @ApiOperation("根据条件查询规范指引")
    @PostMapping("/selectListSchedule")
    public TableDataInfo selectSpecificationGuidelines(
            @RequestBody(required = false) SpecificationGuidelines specificationGuidelines){

        startPage();

        List<SpecificationGuidelines> list = specificationGuidelinesService.selectSpecificationGuidelines(specificationGuidelines);

        return getDataTable(list);
    }
}
